package com.key.controller.admin.assetManagement;

import com.key.common.R;
import com.key.entity.Assets;
import com.key.entity.MainInfoSingle;
import com.key.mapper.MainInfoMapper;
import com.key.service.MainInfoService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: li
 * @CreateTime: 2023-02-18  11:23
 */

@Controller
@RequestMapping("/mainInfo")
@Api(value = "维保信息模块",tags = "维保信息模块接口")
//维保信息模块相关功能
public class MaintenanceInfoController {

    @Autowired
    private MainInfoService mainInfoService;



    /**
     * 添加多个资产的维保信息单
     * @param supplier
     * @param contacts
     * @param contactInfo
     * @param personInCharge
     * @param expirationTime
     * @param explain
     * @param ids
     * @return
     */
    @RequestMapping(value = "/addMainInfo",method = RequestMethod.POST)
    @ApiOperation(value = "维保信息接口",notes = "查询所有维保信息单信息",httpMethod = "post",response = Json.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier", value = "供应商", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "contacts", value = "联系人", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "contactInfo", value = "联系方式", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "personInCharge", value = "负责人", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "expirationTime", value = "维保到期时间", paramType = "query", dataType = "Date", required = true),
            @ApiImplicitParam(name = "explain", value = "维保说明", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "ids", value = "选中的资产id", paramType = "query", dataType = "int[]", required = true,allowMultiple = true)
    })
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R addMainInfo(@RequestParam(name = "supplier") String supplier,
                         @RequestParam(name = "contacts") String contacts,
                         @RequestParam(name = "contactInfo") String contactInfo,
                         @RequestParam(name = "personInCharge") String personInCharge,
                         @RequestParam(name = "expirationTime") Timestamp expirationTime,
                         @RequestParam(name = "explain") String explain,
                         @RequestParam(name = "ids")Integer[] ids){



        Assets assets = new Assets();
        assets.setSupplier(supplier);
        assets.setContacts(contacts);
        assets.setContactInfo(contactInfo);
        assets.setPersonInCharge(personInCharge);
        assets.setExpirationTime(expirationTime);
        assets.setExplain(explain);


        R r = mainInfoService.addMainInfo(assets, ids);

        return r;
    }

    /**
     * 显示维保信息单信息，包括高级搜素
     * @param mainInfoSingle
     * @return
     */
    @RequestMapping(value = "/showAllMainInfo",method = RequestMethod.POST)
    @ApiOperation(value = "维保信息接口",notes = "查询所有维保信息单信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllMainInfo(@RequestBody @ApiParam(name = "mainInfoSingle",value = "维保信息单信息 ",required = false) MainInfoSingle mainInfoSingle){

        R r = mainInfoService.showAllMainInfoSingle(mainInfoSingle);

        return r;
    }

    /**
     * 查询单个维保信息单信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/showMainInfoById",method = RequestMethod.GET)
    @ApiOperation(value = "维保信息接口",notes = "查询单个维保信息单信息",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiImplicitParam(name = "id",value = "选中的维保信息单id",paramType = "query",dataType = "int",required = true)

    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showMainInfoById(@RequestParam(name = "id") Integer id){

        R r = mainInfoService.showMainInfoById(id);

        return r;
    }

    /**
     * 根据维保信息单号查询资产信息
     * @param mainInfoNum
     * @return
     */
    @RequestMapping(value = "/showAssetsByMainInfoId",method = RequestMethod.GET)
    @ApiOperation(value = "维保信息接口",notes = "根据维保单号查询资产信息",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiImplicitParam(name = "mainInfoNum",value = "选中的维保信息单号",paramType = "query",dataType = "String",required = true)

    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAssetsByMainInfoId(@RequestParam(name = "mainInfoNum") String mainInfoNum){

        R r = mainInfoService.showAssetsByMainNum(mainInfoNum);

        return r;
    }

//    /**
//     * 根据业务所属公司名字查询负责人
//     * @param mainCompany
//     * @return
//     */
//    @RequestMapping(value = "/showPersonByCompanyName",method = RequestMethod.GET)
//    @ApiOperation(value = "维保信息接口",notes = "根据业务所属公司名字查询负责人",httpMethod = "get",response = Json.class)
//    @ResponseBody
//    @ApiImplicitParam(name = "mainCompany",value = "选中的业务所属公司名字",paramType = "query",dataType = "int",required = true)
//
//    @ApiResponses({
//            @ApiResponse(code=200,message="请求成功"),
//            @ApiResponse(code=404,message="找不到页面"),
//            @ApiResponse(code=500,message="后台服务错误")
//    })
//    public R showPersonByCompanyName(@RequestParam(name = "mainCompany") String mainCompany){
//
//        R r = new R();
//
//        return r;
//    }
//
//    @RequestMapping(value = "/updateMainInfo",method = RequestMethod.POST)
//    @ApiOperation(value = "维保信息接口",notes = "根据维保单号修改维保信息",httpMethod = "post",response = Json.class)
//    @ResponseBody
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "supplier", value = "供应商", paramType = "query", dataType = "String", required = true),
//            @ApiImplicitParam(name = "contacts", value = "联系人", paramType = "query", dataType = "String", required = true),
//            @ApiImplicitParam(name = "contactInfo", value = "联系方式", paramType = "query", dataType = "String", required = true),
//            @ApiImplicitParam(name = "personInCharge", value = "负责人", paramType = "query", dataType = "String", required = true),
//            @ApiImplicitParam(name = "expirationTime", value = "维保到期时间", paramType = "query", dataType = "Date", required = true),
//            @ApiImplicitParam(name = "explain", value = "维保说明", paramType = "query", dataType = "String", required = true)
//    })    @ApiResponses({
//            @ApiResponse(code=200,message="请求成功"),
//            @ApiResponse(code=404,message="找不到页面"),
//            @ApiResponse(code=500,message="后台服务错误")
//    })
//    public R updateMainInfo(@RequestParam(name = "supplier") String supplier,
//                            @RequestParam(name = "contacts") String contacts,
//                            @RequestParam(name = "contactInfo") String contactInfo,
//                            @RequestParam(name = "personInCharge") String personInCharge,
//                            @RequestParam(name = "expirationTime") Timestamp expirationTime,
//                            @RequestParam(name = "explain") String explain){
//
//        R r = new R();
//
//        return r;
//    }


    /**
     * 根据根据维保单id删除维保单
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteMainInfoById",method = RequestMethod.POST)
    @ApiOperation(value = "资产调拨接口",notes = "根据根据维保单id删除维保单",httpMethod = "post",response = Json.class)
    @ResponseBody

    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R deleteMainInfoById(@RequestBody @ApiParam(name = "ids",value = "选中的维保单id ",required = false) Integer[] ids){

        R r = mainInfoService.deleteMainInfoById(ids);

        return r;
    }

    /**
     * 维保信息导出Excel文件
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/mainInfoExport",method = RequestMethod.GET)
    @ApiOperation(value = "维保信息导出",notes = "维保信息导出",httpMethod = "get",response = Json.class)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = mainInfoService.getWork();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "维保信息单"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
